package com.mali.user.controller;

import com.mali.common.dto.*;
import com.mali.common.web.annotation.IgnoreAuthUserId;
import com.mali.common.web.domain.dto.*;
import com.mali.user.domain.vo.AllUserVO;
import com.mali.user.domain.vo.LoginVO;
import com.mali.user.domain.vo.RegisterVO;
import com.mali.user.domain.vo.UserVO;
import com.mali.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @PostMapping("/code")
    @IgnoreAuthUserId
    public Result<String> getCode(@RequestBody @Validated CodeDTO codeDTO){
        log.debug("获取验证码：手机号：{}", codeDTO.getPhone());
        return userService.getCode(codeDTO);
    }

    @RequestMapping("/register")
    @IgnoreAuthUserId
    public Result<RegisterVO> register(@RequestBody @Validated RegisterDTO registerDTO){
        return userService.register(registerDTO);
    }

    @PostMapping("/login/password")
    @IgnoreAuthUserId
    public Result<LoginVO> login(@RequestBody @Validated PasswordLoginDTO passwordLoginDTO){
        return userService.login(passwordLoginDTO);
    }

    @PostMapping("/login/code")
    @IgnoreAuthUserId
    public Result<LoginVO> login(@RequestBody @Validated CodeLoginDTO codeLoginDTO){
        return userService.login(codeLoginDTO);
    }

    @GetMapping("/users")
    public Result<AllUserVO> getUserList(@RequestBody @Validated PageRequestDTO pageRequestDTO){
        return userService.getUserList(pageRequestDTO);
    }

    @GetMapping("/{userId}")
    public Result<UserVO> getUserInfo(@PathVariable("userId") @NotNull Long userId){
        return userService.getUserInfo(userId);
    }

    @PutMapping("/{userId}")
    public Result<String> updateUser(@PathVariable("userId") @NotNull Long userId, @RequestBody @Validated UserUpdateDTO updateDto){
        return userService.updateUser(userId, updateDto);
    }

    @PostMapping("/reset-password")
    public Result<String> resetPassword(@RequestBody @Validated ResetPwdDTO resetPwdDTO){
        return userService.resetPassword(resetPwdDTO);
    }
}
